namespace ASPNET.Queries
{
    using ASPNET.StarterKit.IssueTracker.BusinessLogicLayer;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Diagnostics;
    using System.Runtime.CompilerServices;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using ASPNET.UserControls;

    public class QueryDetail : Page
    {
        [AccessedThroughProperty("btnaci")]
        private Button _btnaci;
        [AccessedThroughProperty("btnacr")]
        private Button _btnacr;
        [AccessedThroughProperty("btnAddClause")]
        private Button _btnAddClause;
        [AccessedThroughProperty("btnPerformQuery")]
        private Button _btnPerformQuery;
        [AccessedThroughProperty("btnRemoveClause")]
        private Button _btnRemoveClause;
        [AccessedThroughProperty("ctlDisplayIssues")]
        private DisplayIssues _ctlDisplayIssues;
        [AccessedThroughProperty("dropProjects")]
        private PickProject _dropProjects;
        [AccessedThroughProperty("lblError")]
        private Label _lblError;
        [AccessedThroughProperty("lblResult")]
        private Label _lblResult;
        [AccessedThroughProperty("lblSaveError")]
        private Label _lblSaveError;
        [AccessedThroughProperty("plhClauses")]
        private PlaceHolder _plhClauses;
        private object designerPlaceholderDeclaration;

        public QueryDetail()
        {
            base.Init += new EventHandler(this.Page_Init);
            base.Load += new EventHandler(this.Page_Load);
        }

        public void AddClause(bool bindData)
        {
            PickQueryField ctlPickQueryField = (PickQueryField)this.Page.LoadControl("~/UserControls/PickQueryField.ascx");
            this.plhClauses.Controls.Add(ctlPickQueryField);
            ctlPickQueryField.ProjectId = this.dropProjects.SelectedValue;
        }

        public void BindQueryFieldTypes()
        {
            IEnumerator _Vb_t_ref_0 = null;
            try
            {
                _Vb_t_ref_0 = this.plhClauses.Controls.GetEnumerator();
                while (_Vb_t_ref_0.MoveNext())
                {
                    PickQueryField ctlPickQueryField = (PickQueryField)_Vb_t_ref_0.Current;
                    ctlPickQueryField.ProjectId = this.dropProjects.SelectedValue;
                }
            }
            finally
            {
                if (_Vb_t_ref_0 is IDisposable)
                {
                    ((IDisposable)_Vb_t_ref_0).Dispose();
                }
            }
        }

        private void btnacr_Click(object sender, EventArgs e)
        {
            this.Response.Redirect("QueryList.aspx");
        }

        public void btnAddClauseClick(object s, EventArgs e)
        {
            this.ClauseCount++;
            this.AddClause(true);
            this.btnRemoveClause.Enabled = true;
        }

        public void btnPerformQueryClick(object s, EventArgs e)
        {
            this.ctlDisplayIssues.CurrentPageIndex = 0;
            this.ExecuteQuery();
        }

        public void btnRemoveClauseClick(object s, EventArgs e)
        {
            if (this.ClauseCount > 1)
            {
                this.ClauseCount--;
                this.plhClauses.Controls.RemoveAt(this.plhClauses.Controls.Count - 1);
            }
            if (this.ClauseCount < 2)
            {
                this.btnRemoveClause.Enabled = false;
            }
        }

        public QueryClauseCollection BuildQuery()
        {
            QueryClauseCollection colQueryClauses = new QueryClauseCollection();
            foreach (PickQueryField ctlPickQuery in this.plhClauses.Controls)
            {
                QueryClause objQueryClause = ctlPickQuery.QueryClause;
                if (objQueryClause != null)
                {
                    colQueryClauses.Add(objQueryClause);
                }
            }
            return colQueryClauses;
        }

        public void DisplayClauses()
        {
            int _Vb_t_i4_0 = this.ClauseCount - 1;
            for (int i = 0; i <= _Vb_t_i4_0; i++)
            {
                this.AddClause(false);
            }
        }

        public void ExecuteQuery()
        {
            QueryClauseCollection colQueryClauses = this.BuildQuery();
            if (colQueryClauses.Count > 0)
            {
                try
                {
                    IssueCollection colIssues = Issue.PerformQuery(this.dropProjects.SelectedValue, colQueryClauses);
                    this.ctlDisplayIssues.DataSource = colIssues;
                }
                catch (Exception exception1)
                {
                    ProjectData.SetProjectError(exception1);
                    this.lblError.Text = "Error in query. Please check query values.";
                    ProjectData.ClearProjectError();
                }
                this.ctlDisplayIssues.DataBind();
            }
        }

        [DebuggerStepThrough]
        private void InitializeComponent()
        {
        }

        public void IssuesRebind(object s, EventArgs e)
        {
            this.ExecuteQuery();
        }

        private void Page_Init(object sender, EventArgs e)
        {
            this.InitializeComponent();
        }

        private void Page_Load(object sender, EventArgs e)
        {
            this.DisplayClauses();
            if (!this.Page.IsPostBack)
            {
                this.dropProjects.DataSource = Project.GetProjectByMemberUsername(this.User.Identity.Name);
                this.dropProjects.DataBind();
                if (this.dropProjects.SelectedValue == 0)
                {
                    this.Response.Redirect("~/NoProjects.aspx");
                }
                this.BindQueryFieldTypes();
            }
        }

        public void ProjectSelectedIndexChanged(object s, EventArgs e)
        {
            IEnumerator _Vb_t_ref_0 = null;
            this.BindQueryFieldTypes();
            try
            {
                _Vb_t_ref_0 = this.plhClauses.Controls.GetEnumerator();
                while (_Vb_t_ref_0.MoveNext())
                {
                    ((PickQueryField)_Vb_t_ref_0.Current).Clear();
                }
            }
            finally
            {
                if (_Vb_t_ref_0 is IDisposable)
                {
                    ((IDisposable)_Vb_t_ref_0).Dispose();
                }
            }
        }

        protected virtual Button btnaci
        {
            get
            {
                return this._btnaci;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._btnaci != null)
                {
                }
                this._btnaci = value;
                if (this._btnaci != null)
                {
                }
            }
        }

        protected virtual Button btnacr
        {
            get
            {
                return this._btnacr;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._btnacr != null)
                {
                    this._btnacr.Click -= new EventHandler(this.btnacr_Click);
                }
                this._btnacr = value;
                if (this._btnacr != null)
                {
                    this._btnacr.Click += new EventHandler(this.btnacr_Click);
                }
            }
        }

        protected virtual Button btnAddClause
        {
            get
            {
                return this._btnAddClause;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._btnAddClause != null)
                {
                    this._btnAddClause.Click -= new EventHandler(this.btnAddClauseClick);
                }
                this._btnAddClause = value;
                if (this._btnAddClause != null)
                {
                    this._btnAddClause.Click += new EventHandler(this.btnAddClauseClick);
                }
            }
        }

        protected virtual Button btnPerformQuery
        {
            get
            {
                return this._btnPerformQuery;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._btnPerformQuery != null)
                {
                    this._btnPerformQuery.Click -= new EventHandler(this.btnPerformQueryClick);
                }
                this._btnPerformQuery = value;
                if (this._btnPerformQuery != null)
                {
                    this._btnPerformQuery.Click += new EventHandler(this.btnPerformQueryClick);
                }
            }
        }

        protected virtual Button btnRemoveClause
        {
            get
            {
                return this._btnRemoveClause;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._btnRemoveClause != null)
                {
                    this._btnRemoveClause.Click -= new EventHandler(this.btnRemoveClauseClick);
                }
                this._btnRemoveClause = value;
                if (this._btnRemoveClause != null)
                {
                    this._btnRemoveClause.Click += new EventHandler(this.btnRemoveClauseClick);
                }
            }
        }

        public int ClauseCount
        {
            get
            {
                if (this.ViewState["ClauseCount"] == null)
                {
                    return 1;
                }
                return IntegerType.FromObject(this.ViewState["ClauseCount"]);
            }
            set
            {
                this.ViewState["ClauseCount"] = value;
            }
        }

        protected virtual DisplayIssues ctlDisplayIssues
        {
            get
            {
                return this._ctlDisplayIssues;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._ctlDisplayIssues != null)
                {
                    this._ctlDisplayIssues.RebindCommand -= new EventHandler(this.IssuesRebind);
                }
                this._ctlDisplayIssues = value;
                if (this._ctlDisplayIssues != null)
                {
                    this._ctlDisplayIssues.RebindCommand += new EventHandler(this.IssuesRebind);
                }
            }
        }

        protected virtual PickProject dropProjects
        {
            get
            {
                return this._dropProjects;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._dropProjects != null)
                {
                    this._dropProjects.SelectedIndexChanged -= new EventHandler(this.ProjectSelectedIndexChanged);
                }
                this._dropProjects = value;
                if (this._dropProjects != null)
                {
                    this._dropProjects.SelectedIndexChanged += new EventHandler(this.ProjectSelectedIndexChanged);
                }
            }
        }

        protected virtual Label lblError
        {
            get
            {
                return this._lblError;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._lblError != null)
                {
                }
                this._lblError = value;
                if (this._lblError != null)
                {
                }
            }
        }

        protected virtual Label lblResult
        {
            get
            {
                return this._lblResult;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._lblResult != null)
                {
                }
                this._lblResult = value;
                if (this._lblResult != null)
                {
                }
            }
        }

        protected virtual Label lblSaveError
        {
            get
            {
                return this._lblSaveError;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._lblSaveError != null)
                {
                }
                this._lblSaveError = value;
                if (this._lblSaveError != null)
                {
                }
            }
        }

        protected virtual PlaceHolder plhClauses
        {
            get
            {
                return this._plhClauses;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._plhClauses != null)
                {
                }
                this._plhClauses = value;
                if (this._plhClauses != null)
                {
                }
            }
        }
    }
}

